package 优先算法.二分查找.x的平方根;

/**
 * https://leetcode.cn/problems/sqrtx/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        System.out.println(so.mySqrt(2147395599));
    }
}
class Solution {
    public int mySqrt(int x) {
        if(x == 0) return 0;
        long left = 1,right = x;
        while(left < right){
            long mid = left+(right - left + 1)/2;
            if(sqrt(mid)<= x){
                left = mid;
            }
            if(sqrt(mid) > x){
                right = mid - 1;
            }
        }
        return (int)left;
    }
    public static long sqrt(long x){
        return x*x;
    }
}